//
//  CYInvestSucceededViewController.m
//  cyjf
//
//  Created by tauCross on 2017/5/9.
//  Copyright © 2017年 Auto Financing. All rights reserved.
//

#import "CYInvestSucceededViewController.h"

#import "CYInvestSucceededTopCell.h"
#import "CYEmptyCell.h"
#import "CYInvestRecordProgressCell.h"

#import "CYInvestRecordDetailViewController.h"
#import "CYMainController.h"
#import "CYWebViewController.h"



@interface CYInvestSucceededViewController ()

@property(nonatomic, strong)UIView *tableFooterView;
@property(nonatomic, strong)UIButton *invitedButton;
@property(nonatomic, strong)UIButton *financeButton;

@end


@implementation CYInvestSucceededViewController

- (void)loadUI
{
    self.navigationItem.title = @"投资成功";
    
    self.tableView.cy_autoLockVertical = YES;
    
    self.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 162)];
    self.tableView.tableFooterView = self.tableFooterView;
    {
        self.invitedButton = [UIButton cy_mainButtonWithTitle:@"邀请好友，拿奖励"];
        [self.invitedButton addTarget:self action:@selector(onInvitedButtonTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
        [self.tableFooterView addSubview:self.invitedButton];
        
        self.financeButton = [UIButton buttonWithType:UIButtonTypeSystem];
        self.financeButton.tintColor = APP_DEFAULT_COLOR_RED;
        [self.financeButton addTarget:self action:@selector(onFinanceButtonTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
        [self.financeButton setTitle:@"继续投资 >" forState:UIControlStateNormal];
        self.financeButton.titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
        [self.tableFooterView addSubview:self.financeButton];
    }
}

- (void)loadData
{
    NSMutableArray *cells = [NSMutableArray array];
    {
        @weakify(self);
        CYInvestSucceededTopCellObject *cell = [CYInvestSucceededTopCellObject objectWithCellClass:[CYInvestSucceededTopCell class]];
        cell.detailAction = ^{
            @strongify(self);
            [self detailAction];
        };
        [cells addObject:cell];
    }
    {
        CYEmptyCellObject *cell = [CYEmptyCellObject objectWithCellClass:[CYEmptyCell class]];
        cell.height = 10;
        [cells addObject:cell];
    }
    {
        CYInvestRecordProgressCellObject *cell = [CYInvestRecordProgressCellObject objectWithCellClass:[CYInvestRecordProgressCell class]];
        cell.model = self.model;
        [cells addObject:cell];
    }
    self.tableModel = [[NIMutableTableViewModel alloc] initWithSectionedArray:cells delegate:(id)[NICellFactory class]];
    self.tableView.dataSource = self.tableModel;
    [self.tableView reloadData];
}

- (void)layoutUI
{
    self.tableFooterView.width = self.tableView.width;
    {
        self.invitedButton.width = self.tableFooterView.width - 40;
        self.invitedButton.height = 44;
        self.invitedButton.centerX = self.tableFooterView.width / 2;
        self.invitedButton.top = 40;
        
        self.financeButton.frame = self.invitedButton.frame;
        self.financeButton.centerY = self.invitedButton.bottom + 39;
    }
}

#pragma mark - Action
- (void)detailAction
{
    CYInvestRecordDetailViewController *investRecordDetailViewController = [[CYInvestRecordDetailViewController alloc] init];
    investRecordDetailViewController.iid = self.model.iid;
    [self.navigationController pushViewController:investRecordDetailViewController animated:YES];
}

- (void)investAction
{
    [[CYMainController standardController] goFinance];
}

- (void)invitedAction
{
    CYWebViewController *webViewController = [[CYWebViewController alloc] init];
    webViewController.url = [CY_H5_BASE_URL stringByAppendingString:@"views/friends_first.html"];
    [self.navigationController pushViewController:webViewController animated:YES];
}

#pragma mark - Control Events
- (void)onFinanceButtonTouchUpInside
{
    [self investAction];
}

- (void)onInvitedButtonTouchUpInside
{
    [self invitedAction];
}

@end
